/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gracefulmm.dealImage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;

/**
 *
 * @author wenbo
 */
public class DealImage implements Runnable {

    private String imagePath;
    private String imageSize;
    private JDialog dlg;

    public DealImage(String imagePath, String imageSize, JDialog dlg) {
        this.imagePath = imagePath;
        this.imageSize = imageSize;
        this.dlg = dlg;
    }

    @Override
    public void run() {
        File file = new File(imagePath);
        File[] fileLits = file.listFiles();
        if (fileLits != null && fileLits.length > 0) {
            String imageType = null;
            List<Future<Boolean>> listfutures = new ArrayList<Future<Boolean>>();
            ExecutorService executorService = ImageExecutorService.getImageExecutorServiceInstance();
            for (File findFile : fileLits) {
                imageType = ImageTools.getImageType(findFile.getName());
                if ("bmp".equals(imageType) || "jpeg".equals(imageType)
                        || "jpg".equals(imageType) || "png".equals(imageType)
                        || "JPG".equals(imageType)) {
                    if(!ImageTools.isDeal(findFile.getName())){
                        listfutures.add(executorService.submit(new ImageExecutor(findFile.getPath(), imageSize)));
                    }
                }
            }
            if(listfutures.size() > 0){
                for(Future<Boolean> fu:listfutures){
                    try {
                        fu.get();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(DealImage.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ExecutionException ex) {
                        Logger.getLogger(DealImage.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            dlg.setVisible(false);
            
        }
    }
}
